<script setup lang="ts">
import CustomNavbar from '@/pages/index/componets/CustomNavbar.vue'
import CategoryPanel from './componets/CategoryPanel.vue'
import HotPanel from './componets/HotPanel.vue'
import PageSkeleton from './componets/PageSkeleton.vue'
import { onLoad } from '@dcloudio/uni-app'
import { getHomeBannerAPI, getHomeCategoryAPI, getHomeHotAPI } from '@/services/home/home'
import { ref } from 'vue'
import type { BannerItem, CategoryItem, HotItem } from '@/types/home'
import type { XtxGuessInstance } from '@/types/component'
// 存储广告的数据
const bannerList = ref<BannerItem[]>([])
// 存储分类数据
const categoryList = ref<CategoryItem[]>([])
// 存储猜你喜欢
const hotmutilList = ref<HotItem[]>([])
//
const guessRef = ref<XtxGuessInstance>()
// 控制下拉刷新结束的
const isTriggered = ref<boolean>(false)
// 控制骨架屏显示与隐藏(页面加载时触发)
const loading = ref<boolean>(false)

onLoad(async () => {
  loading.value = true
  await Promise.all([getHomeBannerData(), getHomeCategoryData(), getHomeHotData()])
  loading.value = false
})
// 获取首页广告的数据
const getHomeBannerData = async () => {
  let res = await getHomeBannerAPI()
  // console.log(res);
  bannerList.value = res.result
}
// 获取首页分类数据
const getHomeCategoryData = async () => {
  const res = await getHomeCategoryAPI()
  categoryList.value = res.result
}
// 获取猜你喜欢列表
const getHomeHotData = async () => {
  let res = await getHomeHotAPI()
  // console.log(res);
  hotmutilList.value = res.result
}
//获取滚动触底事件
const onScrolltolower = () => {
  console.log(12)

  guessRef.value?.getMore()
}
// 下拉刷新
const onrRefresherrefresh = async () => {
  // 开始动画
  isTriggered.value = true
  // 重置数据
  guessRef.value?.resetData()
  // 可以别调用await但是耗时间，第一完成以后才能进行下一个，使用.all记得里面时调用的方法！！！加括号
  await Promise.all([
    getHomeBannerData(),
    getHomeCategoryData(),
    getHomeHotData(),
    guessRef.value?.getMore(),
  ])
  // 关闭动画
  isTriggered.value = false
}
</script>

<template>
  <!-- 顶部导航栏 -->
  <CustomNavbar></CustomNavbar>
  <!-- 使用scroll-view设置滚动条，让顶部固定，先给滚动条加上高度 ,并绑定一个滚动触底事件-->
  <scroll-view
    refresher-enabled
    @refresherrefresh="onrRefresherrefresh"
    :refresher-triggered="isTriggered"
    scroll-y
    class="scoll-view"
    @scrolltolower="onScrolltolower"
  >
    <!-- 使用骨架屏 -->
    <PageSkeleton v-if="loading"></PageSkeleton>
    <template v-else>
      <!-- 轮播图 -->
      <XtxSwiper :list="bannerList"></XtxSwiper>
      <!-- 轮播图下方分类列表 -->
      <CategoryPanel :list="categoryList"></CategoryPanel>
      <!-- 热门推荐 -->
      <HotPanel :list="hotmutilList"></HotPanel>
      <!-- 猜你喜欢,获取组件实例，进行滚动 -->
      <XtxGuess ref="guessRef"></XtxGuess>
    </template>
  </scroll-view>
</template>

<style lang="scss">
//修改页面颜色
page {
  background-color: #f7f7f7;
  height: 100%;
  display: flex;
  flex-direction: column; //方向社子为
}

.scoll-view {
  flex: 1;
}
</style>
